home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / convrtrs / cutitout.lha / cutitoutwin.c < prev    next >
C/C++ Source or Header  |  1993-01-04  |  11KB  |  407 lines

  1. /*
  2.  *  Source machine generated by GadToolsBox V1.4
  3.  *  which is (c) Copyright 1991,92 Jaba Development
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classes.h>
  9. #include <intuition/classusr.h>
  10. #include <intuition/imageclass.h>
  11. #include <intuition/gadgetclass.h>
  12. #include <libraries/gadtools.h>
  13. #include <graphics/displayinfo.h>
  14. #include <graphics/gfxbase.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <clib/gadtools_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/utility_protos.h>
  20. #include <string.h>
  21.  
  22. #include "cutitoutwin.h"
  23.  
  24. extern int bitgood;
  25. struct Screen         *Scr = NULL;
  26. UBYTE                 *PubScreenName = NULL;
  27. APTR                   VisualInfo = NULL;
  28. struct Window         *CutBitmapWnd = NULL;
  29. struct Gadget         *CutBitmapGList = NULL;
  30. struct IntuiMessage    CutBitmapMsg;
  31. struct Gadget         *CutBitmapGadgets[5];
  32. UWORD                  CutBitmapLeft = 69;
  33. UWORD                  CutBitmapTop = 25;
  34. UWORD                  CutBitmapWidth = 518;
  35. UWORD                  CutBitmapHeight = 169;
  36. UBYTE                 *CutBitmapWdt = (UBYTE *)"Cut-it-out!  ⌐1992 K.Peeters";
  37. struct TextAttr       *Font, Attr;
  38. UWORD                  FontX, FontY;
  39. UWORD                  OffX, OffY;
  40.  
  41. UWORD CutBitmapGTypes[] = {
  42.     BUTTON_KIND,
  43.     BUTTON_KIND,
  44.     BUTTON_KIND,
  45.     BUTTON_KIND,
  46.     BUTTON_KIND
  47. };
  48.  
  49. struct NewGadget CutBitmapNGad[] = {
  50.     10, 151, 83, 13, (UBYTE *)"_Load", NULL, GD_load, PLACETEXT_IN, NULL, (APTR)loadClicked
  51.     109, 151, 83, 13, (UBYTE *)"_Save", NULL, GD_save, PLACETEXT_IN, NULL, (APTR)saveClicked,
  52.     208, 151, 83, 13, (UBYTE *)"_Viewer", NULL, GD_view, PLACETEXT_IN, NULL, (APTR)viewClicked,
  53.     307, 151, 83, 13, (UBYTE *)"E_xpand", NULL, GD_expand, PLACETEXT_IN, NULL, (APTR)expandClicked,
  54.     408, 151, 83, 13, (UBYTE *)"Show _full", NULL, GD_showfull, PLACETEXT_IN, NULL, (APTR)showfullClicked,
  55. };
  56.  
  57. ULONG CutBitmapGTags[] = {
  58.     (GT_Underscore), '_', (TAG_DONE),
  59.     (GT_Underscore), '_', (TAG_DONE),
  60.     (GT_Underscore), '_', (TAG_DONE),
  61.     (GT_Underscore), '_', (TAG_DONE),
  62.     (GT_Underscore), '_', (TAG_DONE)
  63. };
  64.  
  65. static UWORD ComputeX( UWORD value )
  66. {
  67.     return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
  68. }
  69.  
  70. static UWORD ComputeY( UWORD value )
  71. {
  72.     return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  73. }
  74.  
  75. static void ComputeFont( UWORD width, UWORD height )
  76. {
  77.     Font = &Attr;
  78.     Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
  79.     Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
  80.     FontX = Scr->RastPort.Font->tf_XSize;
  81.  
  82.     OffX = Scr->WBorLeft;
  83.     OffY = Scr->RastPort.TxHeight + Scr->WBorTop + 1;
  84.  
  85.     if ( width && height ) {
  86.         if (( ComputeX( width ) + OffX + Scr->WBorRight ) > Scr->Width )
  87.             goto UseTopaz;
  88.         if (( ComputeY( height ) + OffY + Scr->WBorBottom ) > Scr->Height )
  89.             goto UseTopaz;
  90.     }
  91.     return;
  92.  
  93. UseTopaz:
  94.     Font->ta_Name = (STRPTR)"topaz.font";
  95.     FontX = FontY = Font->ta_YSize = 8;
  96. }
  97.  
  98. int SetupScreen( void )
  99. {
  100.     if ( ! ( Scr = LockPubScreen( PubScreenName )))
  101.         return( 1L );
  102.  
  103.     ComputeFont( 0, 0 );
  104.  
  105.     if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
  106.         return( 2L );
  107.  
  108.     return( 0L );
  109. }
  110.  
  111. void CloseDownScreen( void )
  112. {
  113.     if ( VisualInfo ) {
  114.         FreeVisualInfo( VisualInfo );
  115.         VisualInfo = NULL;
  116.     }
  117.  
  118.     if ( Scr        ) {
  119.         UnlockPubScreen( NULL, Scr );
  120.         Scr = NULL;
  121.     }
  122. }
  123.  
  124. void CutBitmapRender( void )
  125. {
  126.     ComputeFont( CutBitmapWidth, CutBitmapHeight );
  127.  
  128.     DrawBevelBox( CutBitmapWnd->RPort, OffX + ComputeX( 10 ),
  129.                     OffY + ComputeY( 5 ),
  130.                     ComputeX(CutBitmapWidth-20) - 2*OffX - 3,
  131.                     ComputeY(CutBitmapHeight-10) - 2*OffY ,
  132.                     GT_VisualInfo, VisualInfo, /* GTBB_Recessed, TRUE, */ TAG_DONE );
  133. }
  134.  
  135. clearwin()
  136.    {
  137.       SetDrMd(CutBitmapWnd->RPort,JAM1);
  138.     SetAPen(CutBitmapWnd->RPort,0L);
  139.     RectFill(CutBitmapWnd->RPort,CutBitmapWnd->BorderLeft,
  140.                 CutBitmapWnd->BorderTop,
  141.                 CutBitmapWnd->Width-CutBitmapWnd->BorderRight-1,
  142.                 CutBitmapWnd->Height-CutBitmapWnd->BorderBottom-1);
  143.     }
  144.  
  145. int HandleCutBitmapIDCMP( void )
  146. {
  147.     struct IntuiMessage    *m;
  148.     struct MenuItem        *n;
  149.     static struct EasyStruct es = 
  150.             { sizeof(struct EasyStruct),
  151.               0,
  152.               "Information",
  153.               "Sorry, you'll have to\nmark an area first.",
  154.               "Ok"
  155.             };
  156.     struct Gadget *g;
  157.     int            (*func)();
  158.     BOOL            running = TRUE;
  159.     BOOL            zooming = FALSE;
  160.     BOOL            marked  = FALSE;
  161.     BOOL            inframe = FALSE;
  162.     int x,y,zoomx[2],zoomy[2];
  163.  
  164.     do
  165.      { Wait(1<<CutBitmapWnd->UserPort->mp_SigBit);
  166.           while( m = GT_GetIMsg( CutBitmapWnd->UserPort )) {
  167.  
  168.         CopyMem(( char * )m, ( char * )&CutBitmapMsg, (long)sizeof( struct IntuiMessage ));
  169.  
  170.         GT_ReplyIMsg( m );
  171.  
  172.         switch ( CutBitmapMsg.Class ) {
  173.             
  174.             case IDCMP_MOUSEMOVE:
  175.                 x=m->MouseX;
  176.                 y=m->MouseY;
  177.                 entrytwo:
  178.                 SetAPen(CutBitmapWnd->RPort,1L);
  179.                 SetDrMd(CutBitmapWnd->RPort,COMPLEMENT);
  180.                 if(!marked)
  181.                        { if(x>leftbevinwin() && x<rightbevinwin() &&
  182.                             y>=topbevinwin() && y<bottombevinwin() )
  183.                         { if(!inframe)
  184.                                  { if(!zooming) drawcross(FALSE,TRUE,x,y);
  185.                                  else          drawrubber(FALSE,TRUE,x,y,zoomx,zoomy);
  186.                                }    
  187.                           else
  188.                                { if(!zooming) drawcross(TRUE,TRUE,x,y);
  189.                                    else          drawrubber(TRUE,TRUE,x,y,zoomx,zoomy);
  190.                                }
  191.                           inframe=TRUE;
  192.                         }
  193.                     else
  194.                         { if(inframe)
  195.                                 { if(!zooming) drawcross(TRUE,FALSE,x,y);
  196.                                   else           drawrubber(TRUE,FALSE,x,y,zoomx,zoomy);
  197.                                 }
  198.                           inframe=FALSE;    
  199.                         }    
  200.                     }
  201.                 SetAPen(CutBitmapWnd->RPort,1L);
  202.                 SetDrMd(CutBitmapWnd->RPort,JAM1);
  203.                 break;
  204.  
  205.             case IDCMP_MOUSEBUTTONS:
  206.                 if(CutBitmapMsg.Code==SELECTUP)
  207.                     { x=m->MouseX;
  208.                       y=m->MouseY;
  209.                       SetAPen(CutBitmapWnd->RPort,1L);
  210.                       SetDrMd(CutBitmapWnd->RPort,COMPLEMENT);
  211.                       if(marked)
  212.                             { drawrubber(TRUE,FALSE,x,y,zoomx,zoomy);
  213.                               marked=FALSE;
  214.                               inframe=FALSE;
  215.                               goto entrytwo;
  216.                             }
  217.                       if(!zooming)
  218.                                { if(x>leftbevinwin() && x<rightbevinwin() &&
  219.                                  y>topbevinwin()  && y<bottombevinwin() )
  220.                                     { zooming=TRUE;
  221.                                       zoomx[0]=x;
  222.                                       zoomy[0]=y;
  223.                                       drawcross(TRUE,FALSE,x,y);    
  224.                                       drawrubber(FALSE,TRUE,x,y,zoomx,zoomy);
  225.                                     }
  226.                             }
  227.                       else
  228.                             { if(x>leftbevinwin() && x<rightbevinwin() && 
  229.                                  y>topbevinwin() && y<bottombevinwin() )
  230.                                     { zooming=FALSE;
  231.                                       marked=TRUE;
  232.                                       zoomx[1]=x;    
  233.                                       zoomy[1]=y;
  234.                                       drawrubber(TRUE,TRUE,x,y,zoomx,zoomy);
  235.                                     }
  236.                                else { zooming=FALSE;    
  237.                                       marked=FALSE;
  238.                                          drawrubber(TRUE,FALSE,x,y,zoomx,zoomy);    
  239.                                          drawcross(FALSE,TRUE,x,y);
  240.                                        }  
  241.                             }
  242.                        SetAPen(CutBitmapWnd->RPort,1L);
  243.                       SetDrMd(CutBitmapWnd->RPort,JAM1);
  244.  
  245.                     }        
  246.                 break;
  247.  
  248.             case IDCMP_NEWSIZE:
  249.                 zooming=FALSE;
  250.                 marked=FALSE;
  251.                 inframe=FALSE;
  252.                 CutBitmapWidth=CutBitmapWnd->Width-OffX-Scr->WBorRight;
  253.                 CutBitmapHeight=CutBitmapWnd->Height-OffY-Scr->WBorBottom;
  254.  
  255.                 clearwin();
  256.                 SetAPen(CutBitmapWnd->RPort,1L);
  257.                 CutBitmapRender();
  258.                 RemoveGList(CutBitmapWnd,CutBitmapGList,-1);
  259.                 FreeGadgets(CutBitmapGList);
  260.                 CutBitmapGList=NULL;
  261.                 g=NULL;
  262.                 if(addgadgets(g)!=0) { running=FALSE; break; }
  263.                 AddGList(CutBitmapWnd,CutBitmapGList,~0,-1,NULL);
  264.                 RefreshGList(CutBitmapGList,CutBitmapWnd,NULL,-1);
  265.                 GT_BeginRefresh( CutBitmapWnd );
  266.                 GT_EndRefresh( CutBitmapWnd, TRUE );
  267.                 refreshit();
  268.                 break;
  269.  
  270.             case    IDCMP_REFRESHWINDOW:
  271.                 zooming=FALSE;
  272.                 GT_BeginRefresh( CutBitmapWnd );
  273.                 CutBitmapRender();
  274.                 GT_EndRefresh( CutBitmapWnd, TRUE );
  275.                 break;
  276.  
  277.             case    IDCMP_CLOSEWINDOW:
  278.                 zooming=FALSE;
  279.                 CloseCutBitmapWindow();
  280.                 running=FALSE;
  281.                 return(FALSE);
  282.                 break;
  283.  
  284.             case    IDCMP_GADGETUP:
  285.             case    IDCMP_GADGETDOWN:
  286.                 zooming=FALSE;
  287.                 inframe=FALSE;
  288.                 func = ( void * )(( struct Gadget * )CutBitmapMsg.IAddress )->UserData;
  289.                 if(func==(void *)saveClicked)
  290.                     { if(marked && bitgood) 
  291.                             { SetDrMd(CutBitmapWnd->RPort,COMPLEMENT);
  292.                               drawrubber(TRUE,FALSE,zoomx[1],zoomy[1],zoomx,zoomy);
  293.                               flip(zoomx);
  294.                               flip(zoomy);
  295.                               saveClicked(zoomx,zoomy);
  296.                               drawrubber(FALSE,TRUE,zoomx[1],zoomy[1],zoomx,zoomy);
  297.                               SetDrMd(CutBitmapWnd->RPort,JAM1);
  298.                             }
  299.                       else EasyRequest(CutBitmapWnd,&es,NULL,NULL);
  300.                     }
  301.                 else { running = func();
  302.                        marked=FALSE;
  303.                       }
  304.                 break;
  305.         }
  306.       }
  307.     } while(running);
  308. }
  309.  
  310. flip(ar)
  311. int *ar;
  312.     {
  313.     int fl;
  314.  
  315.     if(ar[0]>ar[1])
  316.          { fl=ar[0];
  317.           ar[0]=ar[1];
  318.           ar[1]=fl;
  319.         }
  320.     }
  321.  
  322.  
  323. int OpenCutBitmapWindow( void )
  324. {
  325.     int result;
  326.     struct Gadget    *g;
  327.     UWORD        wleft = CutBitmapLeft, wtop = CutBitmapTop, ww, wh;
  328.  
  329.     ComputeFont( CutBitmapWidth, CutBitmapHeight );
  330.  
  331.     ww = ComputeX( CutBitmapWidth );
  332.     wh = ComputeY( CutBitmapHeight );
  333.  
  334.     if (( wleft + ww + OffX + Scr->WBorRight ) > Scr->Width ) wleft = Scr->Width - ww;
  335.     if (( wtop + wh + OffY + Scr->WBorBottom ) > Scr->Height ) wtop = Scr->Height - wh;
  336.  
  337.     if((result=addgadgets(g))!=0) return(result);
  338.  
  339.     if ( ! ( CutBitmapWnd = OpenWindowTags( NULL,
  340.                 WA_Left,    wleft,
  341.                 WA_Top,        wtop,
  342.                 WA_Width,    ww + OffX + Scr->WBorRight,
  343.                 WA_Height,    wh + OffY + Scr->WBorBottom,
  344.                 WA_IDCMP,    BUTTONIDCMP|IDCMP_NEWSIZE|IDCMP_GADGETDOWN|IDCMP_MOUSEMOVE|IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
  345.                 WA_Flags,    WFLG_SIZEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_REPORTMOUSE,
  346.                 WA_Gadgets,    CutBitmapGList,
  347.                 WA_Title,    CutBitmapWdt,
  348.                 WA_ScreenTitle,    "Cut-it-out!",
  349.                 WA_PubScreen,    Scr,
  350.                 WA_MinWidth,    5*ComputeX(95)+10,
  351.                 WA_MinHeight,    80,
  352.                 WA_MaxWidth,    688,
  353.                 WA_MaxHeight,    264,
  354.                 TAG_DONE )))
  355.     return( 4L );
  356.  
  357.     GT_RefreshWindow( CutBitmapWnd, NULL );
  358.  
  359.     CutBitmapRender();
  360.  
  361.     return( 0L );
  362. }
  363.  
  364. addgadgets(g)
  365. struct Gadget *g;
  366.     {
  367.     struct NewGadget    ng;
  368.     UWORD lc,tc;
  369.  
  370.     if ( ! ( g = CreateContext( &CutBitmapGList )))
  371.         return( 1L );
  372.  
  373.     for( lc = 0, tc = 0; lc < CutBitmap_CNT; lc++ ) 
  374.       { CopyMem((char * )&CutBitmapNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
  375.  
  376.         ng.ng_TopEdge    = CutBitmapHeight-18;
  377.         ng.ng_VisualInfo = VisualInfo;
  378.         ng.ng_TextAttr   = Font;
  379.         ng.ng_LeftEdge   = OffX+ComputeX(10+4) + 1 + lc*((bevelwidth()-ComputeX(ng.ng_Width))/4);
  380.         ng.ng_TopEdge    = OffY + ComputeY( ng.ng_TopEdge );
  381.         ng.ng_Width      = ComputeX( ng.ng_Width );
  382.         ng.ng_Height     = ComputeY( ng.ng_Height);
  383.  
  384.         CutBitmapGadgets[ lc ] = g = CreateGadgetA((ULONG)CutBitmapGTypes[ lc ], g, &ng, ( struct TagItem * )&CutBitmapGTags[ tc ] );
  385.  
  386.         while( CutBitmapGTags[ tc ] ) tc += 2;
  387.         tc++;
  388.  
  389.         if ( NOT g )
  390.             return( 2L );
  391.        }
  392.     return(0);
  393.     }
  394.  
  395. void CloseCutBitmapWindow( void )
  396. {
  397.     if ( CutBitmapWnd        ) {
  398.         CloseWindow( CutBitmapWnd );
  399.         CutBitmapWnd = NULL;
  400.     }
  401.  
  402.     if ( CutBitmapGList      ) {
  403.         FreeGadgets( CutBitmapGList );
  404.         CutBitmapGList = NULL;
  405.     }
  406. }
  407.